package cn.wikiflyer.beikang.act.home.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView.ScaleType;
import cn.wikiflyer.beikang.IApplication;
import cn.wikiflyer.beikang.act.TabAct;
import cn.wikiflyer.beikang.act.home.IntentBean;
import cn.wikiflyer.beikang.act.home.NewsAct;
import cn.wikiflyer.beikang.act.user.UserKnowBeikangAct;
import cn.wikiflyer.beikang.bean.HomeBean.BannerBean;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.view.adview.GalleryImageView;

/**
 * 广告适配器
 */
public class ADAdapter extends PagerAdapter {

	private ArrayList<BannerBean> mdata;
	Context mContext;

	public WKApplication app() {
		return (WKApplication) mContext.getApplicationContext();
	}

	public ADAdapter(Context context) {
		mContext = context;
	}

	public void setData(ArrayList<BannerBean> data) {
		this.mdata = data;
		System.out.println("mdata   -  " + mdata);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (mdata == null)
			return 0;
		return mdata.size();
	}

	private int position;

	@Override
	public View instantiateItem(ViewGroup container, int _position) {
		GalleryImageView view = new GalleryImageView(mContext);
		view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		view.setScaleType(ScaleType.CENTER_CROP);

		position = _position;

		BannerBean mBannerBean = mdata.get(_position);
		String item = mBannerBean.ad_image;
		if (item != null) {
			// imageView.setDefault(R.drawable.zhanwei_2);
			view.setUrl(item);
			view.reload();
			view.setTag(position);
		}
		final IntentBean mIBean = new IntentBean(mBannerBean.title,
				mBannerBean.ad_image, mBannerBean.description,
				mBannerBean.inputtime, mBannerBean.catid, mBannerBean.id);
		// view.setTag(url);
		view.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (mIBean.title.contains("跳转到会员体系括号内不可删除")) {
					TabAct.self.setTab(4);
					if (((IApplication) app()).getLoginSession() == null) {
						IApplication.showToast(""+IApplication.loginTxt);
						return;
					}
					new Handler().postDelayed(new Runnable() {
						public void run() {
							Intent intent = new Intent(mContext,
									UserKnowBeikangAct.class);
							mContext.startActivity(intent);
						}
					}, 200);
				} else if (mIBean.title.contains("跳转到产品页括号内不可删除")) {
					TabAct.self.setTab(2);
				} else {
					Intent i = new Intent(mContext, NewsAct.class);
					i.putExtra("bean", mIBean);
					mContext.startActivity(i);
				}
			}
		});
		container.addView(view);
		return view;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		try {
			if (((GalleryImageView) object) != null) {
				((GalleryImageView) object).recycle(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		container.removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	class BtnClick implements OnClickListener {
		String link;
		String[] keywords;
		String lastKey;
		String lastSecond;
		int length;

		public BtnClick(String link) {
			this.link = link;
			keywords = link.split("/");
			length = keywords.length;
			if (length != 0) {
				lastKey = keywords[length - 1];
				lastSecond = keywords[length - 2];
			}
		}

		@Override
		public void onClick(View arg0) {
		}

	}
}
